package com.sdau.zhang.controller;

import com.sdau.zhang.service.IProductService;
import com.sdau.zhang.vo.Product;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.*;
@Controller
@CrossOrigin
@RequestMapping("/upload")
public class uploadController {

    @Autowired
    IProductService productService;

    private List<String> imgList = new ArrayList<String>();


    @RequestMapping("/page")
    public static String page()
    {
        return "Mr_z/Submit.html";
    }


    @RequestMapping("/saveImg")
    @ResponseBody
    public String handleFileUpload(@RequestParam("file") MultipartFile file) {
        if (!file.isEmpty()) {
            try {

                String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
                String name = file.getOriginalFilename();
                String type = name.substring(name.indexOf("."));
                String imgName = uuid+type;
                String path = "/root/img/"+imgName;
                File f = new File(path);
                 if (!f.exists()) {
                System.out.println("not exit");
                  f.createNewFile();
                 }
                System.out.println(f.getAbsoluteFile());
                 FileWriter fw = new FileWriter(f.getAbsoluteFile());
                BufferedWriter out = new BufferedWriter(fw);
                file.transferTo(f);





                imgList.add(imgName);
                for(String img:imgList)
                {
                    System.out.println(img);
                }

                out.write(String.valueOf(file.getBytes()));
                out.flush();
                out.close();


            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return "上传失败," + e.getMessage();
            } catch (IOException e) {
                e.printStackTrace();
                return "上传失败," + e.getMessage();
            }

            return "上传成功";

        }
        else {
            return "上传失败，因为文件是空的.";
        }
    }
    @RequestMapping("/reLoad")
    @ResponseBody
    public String reLoad()
    {
        imgList = new ArrayList<String>();
        return "重置成功";

    }

    @RequestMapping(value = "/subData" ,method = RequestMethod.POST)
    public @ResponseBody String subData(HttpServletRequest request)
    {
       /* System.out.println("我来到了后台");
        String str = request.getParameter("str");
        System.out.println(str);
        JSONArray jsonArray = JSONArray.fromObject(str);
        Map<String,Object> map= new HashMap<String,Object>();
        JSONObject jsonObject;
        for(int i = 0;i<jsonArray.size();i++)
        {
            jsonObject = jsonArray.getJSONObject(i);
            map.put("name", (String) jsonObject.get("name"));
            System.out.print((String) jsonObject.get("name")+" ");
            map.put("value", (String) jsonObject.get("value"));
            System.out.println((String) jsonObject.get("value"));
        }*/
        return "success";
    }
    @RequestMapping(value="/subForm",method = RequestMethod.POST)
    public @ResponseBody String subForm(@RequestBody String jj)
    {
        if(imgList.isEmpty() && imgList.size()==0)
        {
            return "请至少提供一张详情图片";
        }else {
            JSONObject jsonObject = JSONObject.fromObject(jj);
            jsonObject.put("id",null);
            jsonObject.put("img1",imgList.get(0));
            jsonObject.put("img2","");
            jsonObject.put("img3","");
            if(imgList.size()>=2)
            {
                jsonObject.put("img2",imgList.get(1));
            }if(imgList.size()>=3)
            {
                jsonObject.put("img3",imgList.get(2));
            }
           /* for(Object key : jsonObject.keySet()){//开发时查看数据用
                System.out.println("key:"+(String)key);
                Object value = jsonObject.get(key);
                System.out.println("value:"+value);
            }*/
            Product product = (Product)JSONObject.toBean(jsonObject,Product.class);
            int xid = productService.MaxId()+1;
            product.setId(xid);
            productService.insertProduct(product);

            return "success";
        }
    }
}

